package nz.co.senanque.addressbook.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import nz.co.senanque.addressbook.instances.Person;

import org.springframework.transaction.annotation.Transactional;
/**
 * @author Roger Parkinson
 *
 */
public class AddressBookDAO {

	private EntityManager entityManager;

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager)
	{
	this.entityManager = entityManager;
	}

	/**
	* This method is used to create the user
	*/
	@Transactional
	public void createPerson(Person person)
	{
	entityManager.persist(person);
	}

	/**
	* This method is used to get all the user names from the table User Detail
	* @return list of user names
	*/
	@Transactional(readOnly=true)
	public List<Person> getAllPersons()
	{
	return entityManager.createQuery("from nz.co.senanque.addressbook.instances.Person").getResultList();
	}

	/**
	* This method is used to delete an user
	* @param userDetail
	*/
	@Transactional
	public void deleteUser(Person Person)
	{
	entityManager.remove(Person);
	}
	
}
